home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / pcxstrm.cpp < prev    next >
C/C++ Source or Header  |  1994-10-10  |  1KB  |  50 lines

  1. #include <assert.h>
  2. #include <mem.h>
  3. #include "pcxstrm.h"
  4.  
  5. // #define NDEBUG 1
  6.  
  7. pcxstream::pcxstream(FILE * f): pos(0), file(f)
  8.     {
  9.     buffer = new char[BUFFER_LENGTH];
  10.     assert(buffer);
  11.     fread(buffer, 1, BUFFER_LENGTH, file);
  12.     }
  13. ////////////////////
  14. void pcxstream::read(unsigned char * dest, int n)
  15.     {
  16.     if (pos==BUFFER_LENGTH)
  17.     {
  18.     fread(buffer, 1, BUFFER_LENGTH, file);
  19.     pos=0;
  20.     }
  21.  
  22.     int n_from_buf;
  23.     n_from_buf = pos + n < BUFFER_LENGTH ? n : BUFFER_LENGTH - pos;
  24.     n -= n_from_buf;
  25.     if(n_from_buf)
  26.     {
  27.     memcpy(dest, buffer + pos, n_from_buf);
  28.     pos += n_from_buf;
  29.     }
  30.  
  31.     while(n > 0)
  32.     {
  33.     pos = 0;
  34.     fread(buffer, 1, BUFFER_LENGTH, file);
  35.     n_from_buf = n < BUFFER_LENGTH ? n : BUFFER_LENGTH;
  36.     n -= n_from_buf;
  37.     if(n_from_buf)
  38.         {
  39.         memcpy(dest, buffer + pos, n_from_buf);
  40.         pos += n_from_buf;
  41.         }
  42.     }
  43.     }
  44. ////////////////////////
  45. pcxstream::~pcxstream()
  46.     {
  47.     delete buffer;
  48.     }
  49. ////////////////////////
  50.